//
//  EpointBrowerViewController.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-1-10.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointBrowerViewController.h"

@interface EpointBrowerViewController ()<UIWebViewDelegate,UIGestureRecognizerDelegate>
{
    UIWebView *mWebView;
    UIButton *hideButton;
    
    MBProgressHUD *MBPHUD;
}
@end

@implementation EpointBrowerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *urlStr = [self.passParam objectForKey:@"url"];
    
    mWebView = [[UIWebView alloc]initWithFrame:self.getFullContentFrame];
    mWebView.backgroundColor = [UIColor clearColor];

    NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"%@",urlStr]];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [mWebView loadRequest:request];
    mWebView.delegate=self;
    [self.view addSubview:mWebView];
    
    
//    hideButton = [[UIButton alloc]initWithFrame:CGRectMake(AppDeviceWidth-60, 20, 60, 30)];
//    [hideButton setBackgroundColor:[UIColor orangeColor]];
//    [hideButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [hideButton.titleLabel setFont:[UIFont systemFontOfSize:12]];
//    [hideButton setTitle:@"全屏" forState:UIControlStateNormal];
//    [hideButton addTarget:self action:@selector(hiddenOrShow) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:hideButton];

    //注册手势
    [self registerGestureAction];
    
    MBPHUD = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:MBPHUD];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_back" highImageName:@"navigationbar_back_highlighted" target:self action:@selector(clickBack)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_home" highImageName:@"navigationbar_home_highlighted" target:self action:@selector(clickBackMainView)];
}

#pragma 返回主页
-(void)clickBackMainView{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma 返回操作
-(void)clickBack{
    //    [self.navigationController popViewControllerAnimated:YES];
    if ([mWebView canGoBack]) {
        [mWebView goBack];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [mWebView stopLoading];
}


#pragma mark - UIWebViewDelegate
/**
 *  UIWebView开始加载资源的时候调用(开始发送请求)
 */
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidStartLoad...");
    //    [self showMBPHUD];
    [MBPHUD show:YES];
}

/**
 *  UIWebView加载完毕的时候调用(请求完毕)
 */
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidFinishLoad...");
    //    [self hideMBPHUD];
    [MBPHUD hide:YES];
}

/**
 *  UIWebView加载失败的时候调用(请求失败)
 */
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"didFailLoadWithError...");
    //    [self hideMBPHUD];
    [MBPHUD hide:YES];
}


#pragma mark UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSString *requestString = [[request URL] absoluteString];
    NSArray *components = [requestString componentsSeparatedByString:@":"];
    if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"testapp"]) {
        if([(NSString *)[components objectAtIndex:1] isEqualToString:@"alert"])
        {
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"Alert from Cocoa Touch" message:[components objectAtIndex:2]
                                  delegate:self cancelButtonTitle:nil
                                  otherButtonTitles:@"OK", nil];
            [alert show];
        }
        return NO;
    }
    return YES;
}


#pragma 手势监听:单击还是双击
-(void) registerGestureAction{
    
    UITapGestureRecognizer* singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
    //点击的次数
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    singleRecognizer.delegate = self;
    
    [mWebView addGestureRecognizer:singleRecognizer];
    
    // 双击的 Recognizer
    UITapGestureRecognizer* doubleRecognizer;
    doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DoubleTap:)];
    doubleRecognizer.numberOfTapsRequired = 2; // 双击
    doubleRecognizer.delegate = self;
    [mWebView addGestureRecognizer:doubleRecognizer];
    
    // 关键在这一行，双击手势确定监测失败才会触发单击手势的相应操作
    [singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

//单击操作
-(void)SingleTap:(UITapGestureRecognizer*)recognizer
{
}
//双击操作
-(void)DoubleTap:(UITapGestureRecognizer*)recognizer
{
    [self hiddenOrShow];//是否全屏
}
#pragma mark - Target Action
- (void)hiddenOrShow
{
    if (self.navigationController.navigationBarHidden) {
        [hideButton setTitle:@"全屏" forState:UIControlStateNormal];
        mWebView.frame =self.getFullContentFrame;
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }else {
        [hideButton setTitle:@"退出全屏" forState:UIControlStateNormal];
        mWebView.frame = CGRectMake(0, 0, AppDeviceWidth, AppHeight);
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}



@end
